package templestowe.grails
import groovy.xml.MarkupBuilder

class Tld {
	
	static final MARKUP_LIST = [
	'actionSubmit':['*value','action'],
	'applyLayout':['*name' ,'*url', 'template','bean','model','collection'],
	'checkBox':['*name','value','checked'],
	'collect':['*in','*expr'],
	'cookie':['*name'],
	'country':['*code'],
	'countrySelect':['*from','value','default','noSelection','valueMessagePrefix'],
	'createLink':['action','controller','id','fragment','mapping','params','url','absolute','base'],
	'createLinkTo':['dir','file','absolute','base'],
	'currencySelect':['from','value'],
	'datePicker':['*name','value','precision','noSelection','years'],
	'each':['*in','status','var'],
	'eachError':['bean','model','field'],
	'else':[],
	'elseif':['test','env'],
	'fieldValue':['*bean','*field'],
	'findAll':['*in','*expr'],
	'form':['action','controller','id','url','name','useToken'],
	'formRemote':['*url','action','update','before','after','asynchronous','method','onSuccess','onFailure','onUninitialized','onLoading','onLoaded','onComplete'],
	'formatBoolean':['*boolean'],
	'formatDate':['*date','format','formatName','type','timeZone','locale','style','dateStyle','timeStyle'],
	'formatNumber':['*number','format','formatName','type','locale','groupingUsed','minIntegerDigits','maxIntegerDigits','minFractionDigits','maxFractionDigits','currencyCode','currencySymbol','roundingMode'],
	'grep':['*in','*filter'],
	'hasErrors':['bean','model','field'],
	'headers':['*name'],
	'hiddenField':['*name','*value'],
	'if':['test','env'],
	'include':['action','controller','id','view','params','model'],
	'javascript':['contextPath','library','src','base','plugin'],
	'layoutBody':[],
	'layoutHead':[],
	'layoutTitle':['default'],
	'link':['action','controller','elementId','id','fragment','mapping','params','url','absolute','base'],
	'localeSelect':['*name','value'],
	'message':['error','code','message','default','args','encodeAs'],
	'meta':['*name'],
	'pageProperty':[],
	'paginate':['*total','action','controller','id','params','prev','next','max','maxsteps','offset'],
	'passwordField':['*name','value'],
	'radio':['*value','*name','checked'],
	'radioGroup':['*name','*values','value','labels'],
	'remoteField':['*name','value','paramName','action','controller','id','update','before','after','asynchronous','method','onSuccess','onFailure','onUninitialized','onLoading','onLoaded','onComplete'],
	'remoteFunction':['action','controller','id','update','before','after','asynchronous','method','onSuccess','onFailure','onUninitialized','onLoading','onLoaded','onComplete'],
	'remoteLink':['action','controller','id','fragment','mapping','params','update','before','after','asynchronous','method','onSuccess','onFailure','onUninitialized','onLoading','onLoaded','onComplete'],
	'render':['contextPath','*template','bean','model','collection','var','plugin'],
	'renderErrors':['as','bean','model','field'],
	'resource':['base','contextPath','dir','file','absolute','plugin'],
	'select':['*from','value','optionKey','optionValue','keys','noSelection','valueMessagePrefix'],
	'set':['*var','*value','scope'],
	'sortableColumn':['*property','defaultOrder','title','titleKey','params','action'],
	'submitButton':['*name','*value'],
	'submitToRemote':['*url','update','before','after','asynchronous','method','onSuccess','onFailure','onUninitialized','onLoading','onLoaded','onComplete'],
	'textArea':['*name','value'],
	'textField':['*name','value'],
	'timeZoneSelect':['*name','value'],
	'uploadForm':['action','controller','id','url'],
	'while':['test'],
	]
	
	float version = 0.2
	
	def write(Writer w) {
		def m = new MarkupBuilder(w)
		m.taglib(
				'xmlns':"http://java.sun.com/xml/ns/j2ee",
				'xmlns:xsi':"http://www.w3.org/2001/XMLSchema-instance",
				'xsi:schemaLocation':"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd",
				'version':"2.0"
				) {
					description("A Grails custom tag library")
					'tlib-version'(version)
					'short-name'('grails')
					uri('http://grails.codehaus.org/tags')
					MARKUP_LIST.each { k,v ->
						tag {
							name(k)
							'tag-class'('org.codehaus.groovy.grails.web.taglib.jsp.JspLinkTag')
							'body-content'('JSP')
							v.each { k2 -> 
								attribute {
									if (k2.startsWith('*')) {
										name(k2.substring(1))
										required('true')
									}
									else {
										name(k2)
										required('false')
									}
									rtexprvalue('true')
								}
							}
						}
					}
				}
	}
}

new Tld().write new PrintWriter(System.out)
